#ifndef BLE_CONFIG_H
#define BLE_CONFIG_H

#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "lwip/ip4_addr.h"
#include "esp_netif.h"
#include "esp_bt_device.h"
#include <string.h>
#include "wifi.h"
// // 添加寄存器操作必需的头文件
// #include "soc/soc.h"
// #include "soc/rtc_cntl_reg.h"  // 包含RTC_CNTL相关寄存器定义
// #include "soc/rtc.h"

// BLE服务UUID
#define GATTS_SERVICE_UUID          0x00FF
#define GATTS_CHAR_UUID_UUID_WIFI_CONFIG 0xFF01
#define GATTS_CHAR_UUID_PASSWORD_WIFI_CONFIG 0xFF02
#define GATTS_CHAR_UUID_IP_CONFIG   0xFF03
#define GATTS_CHAR_UUID_GATEWAY_CONFIG 0xFF04
#define GATTS_CHAR_UUID_SUBNET_CONFIG 0xFF05
//#define GATTS_CHAR_UUID_STATUS      0xFF03

// 设备名称
#define DEVICE_NAME "ESP32_Config"

// 在全局变量中添加
static uint8_t current_char_index = 0;

// 使用顺序跟踪而不是UUID查询
static uint8_t char_add_step = 0;

// WiFi配置结构
typedef struct {
    char ssid[32];
    char password[64];
} my_wifi_config_t;

// 网络配置结构
typedef struct {
    char ip[16];
    char gateway[16];
    char subnet[16];
    // char dns[16];
    uint8_t use_dhcp;
} my_network_config_t;

// BLE服务句柄
typedef struct {
    uint16_t gatts_if;
    uint16_t app_id;
    uint16_t conn_id;
    uint16_t service_handle;
    esp_gatt_srvc_id_t service_id;
    uint16_t char_handle_uuid_wifi;
    uint16_t char_handle_password_wifi;
    // esp_bt_uuid_t char_uuid_wifi;
    uint16_t char_handle_ip;
    uint16_t char_handle_gateway;
    uint16_t char_handle_subnet;
    // esp_bt_uuid_t char_uuid_ip;
    uint16_t char_handle_status;
    esp_bt_uuid_t char_uuid_status;
    uint16_t descr_handle;
    esp_bt_uuid_t descr_uuid;
} ble_gatts_t;

// 全局函数声明
void ble_init(void);
void start_ble(void);
void update_status(const char *status);
void save_wifi_config(const my_wifi_config_t *config);
void save_network_config(const my_network_config_t *config);
void load_config_from_nvs(void);
// void stop_ble(void);
// void ble_deinit(void);

// 全局变量
static ble_gatts_t gatts_profile;
extern my_wifi_config_t wifi_config;
extern my_network_config_t network_config;
static bool ble_connected = false;
static esp_netif_t *netif = NULL;

// NVS命名空间
#define NVS_NAMESPACE "net_config"

#endif